<template>
  <div class="expert-layout">
    <el-container>
      <el-header height="60px">
        <div class="header-content">
          <div class="logo">
            <el-icon :size="32"><FirstAidKit /></el-icon>
            <span>儿童健康专家平台</span>
            <div class="logo-decoration"></div>
          </div>

          <!-- 在线状态切换 -->
          <div class="status-toggle">
            <span class="status-label">在线状态：</span>
            <el-switch
              v-model="isOnline"
              active-text="在线"
              inactive-text="离线"
              @change="handleStatusChange"
            />
          </div>

          <!-- 用户信息 -->
          <div class="user-section">
            <el-dropdown>
              <div class="user-info">
                <el-avatar :size="36" :src="expertAvatar" />
                <div class="expert-info">
                  <span class="expert-name">{{ realName }}</span>
                  <span class="expert-title">{{ title }} | {{ department }}</span>
                </div>
                <el-icon><ArrowDown /></el-icon>
              </div>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="handleLogout">
                    <el-icon><SwitchButton /></el-icon>
                    退出登录
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </el-header>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { FirstAidKit, ArrowDown, SwitchButton } from '@element-plus/icons-vue'
import { removeToken, parseToken } from '@/utils/auth'
import { updateStatus, getExpertInfo } from '@/api/expert'

const router = useRouter()
const defaultAvatar = 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'

// 专家信息
const expertName = ref('') // 用户名
const realName = ref('') // 专家姓名
const title = ref('') // 职称
const department = ref('') // 科室
const expertAvatar = ref(defaultAvatar)
const isOnline = ref(false)

// 初始化用户信息
const initExpertInfo = async () => {
  try {
    const expertInfo = await getExpertInfo()
    expertName.value = expertInfo.username
    realName.value = expertInfo.name
    title.value = expertInfo.title
    department.value = expertInfo.department
    expertAvatar.value = expertInfo.avatar || defaultAvatar
    isOnline.value = expertInfo.status === 1
  } catch (error) {
    console.error('获取专家信息失败:', error)
    ElMessage.error('获取专家信息失败')
  }
}

onMounted(() => {
  initExpertInfo()
})

// 处理在线状态变更
const handleStatusChange = async (value) => {
  try {
    await updateStatus(value)
    ElMessage.success(`已${value ? '上线' : '下线'}`)
  } catch (error) {
    console.error('更新在线状态失败:', error)
    ElMessage.error('更新在线状态失败')
    // 恢复开关状态
    isOnline.value = !value
  }
}

// 退出登录
const handleLogout = () => {
  removeToken()
  ElMessage.success('退出成功')
  router.push('/login')
}
</script>

<style scoped>
.expert-layout {
  min-height: 100vh;
  background: linear-gradient(to bottom, #f7f9fc, #ffffff);
}

.el-header {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  position: fixed;
  width: 100%;
  z-index: 100;
  padding: 0;
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
}

.logo {
  display: flex;
  align-items: center;
  font-size: 20px;
  color: #409EFF;
  font-weight: bold;
  position: relative;
}

.logo .el-icon {
  margin-right: 12px;
  color: #409EFF;
}

.logo-decoration {
  position: absolute;
  bottom: -8px;
  left: 0;
  width: 100%;
  height: 3px;
  background: linear-gradient(90deg, #409EFF, transparent);
  border-radius: 2px;
}

.status-toggle {
  display: flex;
  align-items: center;
  gap: 12px;
}

.status-label {
  font-size: 14px;
  color: #666;
}

.user-section {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 4px 16px;
  border-radius: 25px;
  transition: all 0.3s;
  border: 1px solid transparent;
}

.user-info:hover {
  background: #f0f7ff;
  border-color: var(--el-color-primary-light-7);
}

.expert-info {
  display: flex;
  flex-direction: column;
  margin: 0 8px;
}

.expert-name {
  font-size: 15px;
  font-weight: 500;
  color: #333;
  line-height: 1.2;
}

.expert-title {
  font-size: 12px;
  color: #666;
  line-height: 1.2;
}

.el-main {
  padding-top: 80px;
  padding-bottom: 24px;
}

:deep(.el-dropdown-menu__item) {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
}

:deep(.el-dropdown-menu__item .el-icon) {
  font-size: 16px;
}
</style> 